package app.cn.com.school.activity

import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.os.Handler
import android.os.Message
import android.text.TextUtils
import android.widget.Toast
import app.cn.com.school.R
import app.cn.com.school.api.AppRequest
import app.cn.com.school.api.bean.response.LoginResponse
import app.cn.com.school.config.AppContext
import app.cn.com.school.databinding.ActivityLoginBinding
import app.cn.com.school.tools.*
import app.cn.com.watering.api.ApiUtill
import com.umeng.socialize.UMShareAPI
import com.umeng.socialize.bean.SHARE_MEDIA

class LoginActivity : BaseActivity<ActivityLoginBinding>() {
    override fun initBindingVar() {
    }

    override fun initTitle() {
        binding.titlebar.back.setOnClickListener { finish() }
        binding.titlebar.title.text = "登录"
    }

    var userNmae = ""
    var passWord = ""
    lateinit private var mHandler: Handler
    lateinit var context: Context
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        initBinding(R.layout.activity_login)
        context = this
        initView()
        binding.loginWx.setOnClickListener {
            UtilsUmeng.Login(this@LoginActivity, applicationContext, SHARE_MEDIA.QQ, mHandler)
            SharedPreferenceUtil.insert("logType", "qq")
        }
        binding.loginQq.setOnClickListener {
            UtilsUmeng.Login(this@LoginActivity, applicationContext, SHARE_MEDIA.WEIXIN, mHandler)
            SharedPreferenceUtil.insert("logType", "wx")
        }
        binding.btnLogin.setOnClickListener {
            isNull()

        }
        binding.signNumber.setOnClickListener {
            context.startActivity(Intent(context, SignActivity::class.java))
        }
        binding.signFotPwd.setOnClickListener {
            context.startActivity(Intent(context, PwdActivity::class.java).putExtra("type", "0"))
        }


        // context.startActivity(Intent(context, PwdActivity::class.java).putExtra("type","1"))  第三方登录绑定手机号

    }

    /**
     * 加载控件
     * */
    private fun initView() {
        if (SharedPreferenceUtil.hasKey("userName")) {
            userNmae = SharedPreferenceUtil.read("userName", "")
            passWord = SharedPreferenceUtil.read("passWord", "")
            loginHttp()
        } else if (SharedPreferenceUtil.hasKey("openid")) {
//            httpLoginQQ()
        }

        mHandler = object : Handler() {
            override fun handleMessage(msg: Message) {
                when (msg.what) {
                    Ini.SDK_PAY_FLAG3 -> {
//                        httpLoginQQ()
                    }
                }
            }
        }
    }

    /**
     * 非空判断
     * */
    private fun isNull() {
        var isNull = true
        var msg = ""
        if (!Constants.validMobile(binding.userName.text.toString().trim())) {
            isNull = false
            msg = "请输入正确的手机号"
        }
        if (TextUtils.isEmpty(binding.userPwd.text.toString().trim())) {
            isNull = false
            msg = "请输入密码"
        }
        if (isNull) {
            userNmae = binding.userName.text.toString().trim()
            passWord = binding.userPwd.text.toString().trim()
            loginHttp()
        } else {
            showTips(msg)
            return
        }

    }


    private fun loginHttp() {
        ApiUtill.getInstance().getApi(AppRequest.getAPI().login(userNmae, passWord), { baseResponse ->
            baseResponse as LoginResponse
            if (baseResponse.code == 200) {
                showTips("登录成功")
                if (!SharedPreferenceUtil.hasKey("userName")) {
                    SharedPreferenceUtil.insert("userName", userNmae)
                    SharedPreferenceUtil.insert("passWord", passWord)
                    SharedPreferenceUtil.insert("key", baseResponse.datas.data.key)
                }
                context.startActivity(Intent(context, HomeActivity::class.java))
            } else {
                showTips(baseResponse.datas.error)
            }

        })

    }


    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        UMShareAPI.get(this).onActivityResult(requestCode, resultCode, data)
    }

    override fun onResume() {
        super.onResume()
        AppManager.getAppManager().addActivity(this)
    }

}
